﻿using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 枪的父类
/// </summary>
public abstract class HGun :MonoBehaviour
{
    [Tooltip("子弹")]
    public GameObject bulletPrefab;
    [Tooltip("射速")]
    public float fireRate = 5;
    [Tooltip("子弹速度")]
    public float bulletSpeed = 10;
    [Tooltip("发射点")]
    protected Transform[] firePoints;

    protected float interval = 0;
    protected float lastFireTime = 0;

    public virtual void Awake()
    {
        //获取发射点
        firePoints = new Transform[transform.childCount];
        for (int i = 0; i < firePoints.Length; i++)
        {
            firePoints[i] = transform.GetChild(i);
        }
    }

    public bool CanFire(float lastTime)
    {
        interval = 1f / fireRate;
        if (Time.time - lastTime > interval)
            return true;
        return false;
    }

    public abstract void Fire();

    public virtual void CreateBullet(Transform point)
    {
        CreateBullet(point.position, point.rotation);
    }

    public virtual void CreateBullet(Vector3 pos,Quaternion rot)
    {
        GameObject bullet = GameObject.Instantiate(bulletPrefab);
        bullet.transform.position = pos;
        bullet.transform.rotation = rot;

        bullet.GetComponent<Bullet>().Fire(bulletSpeed);
    }
}
